package com.xyy.yueshijia;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.usher.greendao_demo.greendao.gen.GoodsUserDao;
import com.usher.greendao_demo.greendao.gen.greendao.GreenDaoUtils;
import com.xyy.yueshijia.bean.GoodsUser;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.id;

public class GoodsUserActivity extends AppCompatActivity {
    private EditText editname;
    private EditText editimage;
    private Button btnzengjia;
    private  Button btnshan;
    private  Button btncha;
    private TextView textxianshi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_user);
        final GoodsUserDao userDao = GreenDaoUtils.getInstance(this).getUserDao();
        final ArrayList<GoodsUser> users=new ArrayList<>();
       for (int i = 0; i < 10 ; i++) {
            GoodsUser u=new GoodsUser();
            u.setName("AAAAA"+i);
            u.setImage("测试一下"+i+10);
           users.add(u);
        }
            userDao.insertInTx(users);

        initView();

        btnzengjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  String name=editname.getText().toString();
                  String image=editimage.getText().toString();
                if (!name.isEmpty()&&!image.isEmpty()) {
                    ArrayList<GoodsUser> user2 = new ArrayList<>();
                    GoodsUser u2 = new GoodsUser();
                    u2.setName(name);
                    u2.setImage(image);
                    user2.add(u2);
                    List<GoodsUser> list =userDao.loadAll();
               if (list.size()>0){
                   userDao.insertInTx(user2);
                   Toast.makeText(GoodsUserActivity.this, "增加成功", Toast.LENGTH_SHORT).show();
               } else {
                   Toast.makeText(GoodsUserActivity.this, "增加失败", Toast.LENGTH_SHORT).show();
               }

                }
                else{
                    if (name.isEmpty()&&image.isEmpty()){
                        Toast.makeText(GoodsUserActivity.this, "请填写信息", Toast.LENGTH_SHORT).show();
                    }
                    if (!name.isEmpty()&&image.isEmpty()){
                        Toast.makeText(GoodsUserActivity.this, "请填写完整", Toast.LENGTH_SHORT).show();
                    }
                    if (name.isEmpty()&&!image.isEmpty()){
                        Toast.makeText(GoodsUserActivity.this, "请填写完整", Toast.LENGTH_SHORT).show();
                    }
                }
                editname.setText("");
                editimage.setText("");

            }
        });

          btncha.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                 //  List<GoodsUser> listss =userDao.loadAll();
                 //  Toast.makeText(GoodsUserActivity.this, listss.get(1).getName().toString(), Toast.LENGTH_SHORT).show();
                 //  List<GoodsUser> list = userDao.queryBuilder()
                           //  .limit(10)  //查询10条数据
                           //  .offset(10) //查询时进行偏移
                           //  .orderAsc(UserDao.Properties.Sex)//SEX  升序
                         //  .orderDesc(GoodsUserDao.Properties._id)//AGE 降序
//                .where(UserDao.Properties.Age.gt(15),//年龄大于15,
//                        UserDao.Properties.Age.lt(17),//小于17
//                        UserDao.Properties.Name.like("A%")) //模糊查询
//                .and()
                         //  .whereOr(GoodsUserDao.Properties._id.between(3,10),
                              //     GoodsUserDao.Properties._id.in(12))
                         //  .list();
                  // Toast.makeText(GoodsUserActivity.this, list.toString(), Toast.LENGTH_LONG).show();
                   List<GoodsUser> lists =userDao.loadAll();
                textxianshi.setText(lists.toString());
               }
           });


    }

    private void initView() {
        editname= (EditText) findViewById(R.id.home_editText);
        editimage= (EditText) findViewById(R.id.home_editText2);
        btnzengjia= (Button) findViewById(R.id.zenjiabtn);
        btncha= (Button) findViewById(R.id.chaxunbtn);
        btnshan= (Button) findViewById(R.id.shanchubtn);
        textxianshi= (TextView) findViewById(R.id.home_chet);
    }


}
